﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using PlanningGIS.Engine.Controls;
using PlanningGIS.MapSheet;
using PlanningGIS.MetaData;
using PlanningGIS.Util.Log;
using PlanningGIS.Win.Controls;

namespace PlanningGIS.Cartographic
{
    /// <summary>
    /// 按照图幅输出
    /// </summary>
    public class FrameExport : BigFrame
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public FrameExport()
        {
            UnitValue = SheetHelper.Config.UnitValue;
            SecurityLevel = MetaHelper.FrameConfig.SecurityLevel;
            UnitName = MetaHelper.FrameConfig.UnitName;
        }

        /// <summary>
        /// 需要导出的图幅
        /// </summary>
        public List<Mapsheet> Sheets { get; set; }

        /// <summary>
        /// 自定义的元数据信息
        /// </summary>
        public Dictionary<string, string> MetaValues { get; set; }
        
        /// <summary>
        /// 是否又元数据信息
        /// </summary>
        public bool HasMeta { get; set; }

        /// <summary>
        /// 导出操作对象
        /// </summary>
        private IExport _Export = null;

        /// <summary>
        /// 
        /// </summary>
        private string _ExportType = "";

        /// <summary>
        /// 导出类型
        /// </summary>
        public string ExportType
        {
            set
            {
                _ExportType = value;
                switch (value.ToUpper())
                {
                    case "JPG":
                        _Export = (IExport)new ExportJPEG();
                        break;
                    case "BMP":
                        _Export = (IExport)new ExportBMP();
                        break;
                    case "TIF":
                        _Export = (IExport)new ExportTIFF();
                        break;
                    case "PDF":
                        _Export = (IExport)new ExportPDF();
                        break;
                    default:
                        _Export = (IExport)new ExportJPEG();
                        break;
                }
            }
        }

        /// <summary>
        /// 导出路径
        /// </summary>
        public string ExportPath { get; set; }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sheet"></param>
        public void DrawFrameItem(Mapsheet sheet)
        {
            if (sheet == null) return;
            _LeftValues = new List<string>();
            _RightValues = new List<string>();
            if (HasMeta) MetaHelper.GetMetaInfo(sheet.Code, null, MetaValues, out _LeftValues, out _RightValues);
            Sheet = sheet;
            PageLayout.ZoomToWholePage();
            DrawLayout();
            PageLayout.Refresh();
        }

        /// <summary>
        /// 打印当前选择的图幅
        /// </summary>
        /// <param name="sheet">需要打印的图幅</param>
        public void PrintCurrent(Mapsheet sheet)
        {
            FrmPrint frm = new FrmPrint();
            frm.PageLayerOut = PageLayout;
            if (frm.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;
            PageLayout.Page.PageToPrinterMapping = frm.PrinterMapping;
            PageLayout.Printer.PrintToFile = frm.PrintFileName;
            DrawFrameItem(sheet);
            _Progressor.SetMaxValue(frm.PrintCount);
            for (int i = 1; i <= frm.PrintCount; i++)
            {
                _Progressor.Step();
                PageLayout.PrintPageLayout();
            }
            _Progressor.Stop();
        }

        /// <summary>
        /// 打印所有的图幅
        /// </summary>
        public void PrintAll()
        {
            FrmPrint frm = new FrmPrint();
            frm.PageLayerOut = PageLayout;
            if (frm.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;
            PageLayout.MousePointer = esriControlsMousePointer.esriPointerHourglass;//设置鼠标
            PageLayout.Page.PageToPrinterMapping = frm.PrinterMapping;
            PageLayout.Printer.PrintToFile = frm.PrintFileName;
            _Progressor.SetMaxValue(frm.PrintCount);
            foreach (Mapsheet sheet in Sheets)
            {
                DrawFrameItem(sheet);
                for (int i = 1; i <= frm.PrintCount; i++)
                {
                    _Progressor.Step();
                    PageLayout.PrintPageLayout();
                }
            }            
            _Progressor.Stop();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sheet"></param>
        public void ExportCurrent(Mapsheet sheet)
        {
            DrawFrameItem(sheet);
            Export2Pic(sheet.Code);
        }

        /// <summary>
        /// 
        /// </summary>
        public void ExportAll()
        {
            foreach (Mapsheet sheet in Sheets)
            {
                DrawFrameItem(sheet);
                Export2Pic(sheet.Code);
            }
        }

        /// <summary>
        /// 导出图片
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        private bool Export2Pic(string code)
        {
            if (_Export == null) return false;
            IActiveView _View = PageLayout.ActiveView;
            IEnvelope driverBounds = new Envelope() as IEnvelope;
            tagRECT userRECT;
            int resolution = 96;
            IWorldFileSettings worldFileSetting;
            //输入输出分辨率
            double screenResolution = _View.ScreenDisplay.DisplayTransformation.Resolution;
            LogHelper.WriteLog("正在对[" + code + "]进行导出...");
            string fullname = ExportPath + "\\" + code + "." + _ExportType;
            try
            {
                if (_ExportType.ToUpper() != "PDF")//PDF格式不可导出坐标文件                
                {
                    worldFileSetting = (IWorldFileSettings)_Export;
                    worldFileSetting.OutputWorldFile = false;
                    worldFileSetting.MapExtent = _View.Extent;
                }
                _Export.ExportFileName = fullname;
                _Export.Resolution = resolution;
                userRECT.left = 0;
                userRECT.top = 0;
                userRECT.right = Convert.ToInt32(_View.ExportFrame.right * (resolution / screenResolution));
                userRECT.bottom = Convert.ToInt32(_View.ExportFrame.bottom * (resolution / screenResolution));
                driverBounds.PutCoords(userRECT.left, userRECT.top, userRECT.right, userRECT.bottom);
                _Export.PixelBounds = driverBounds;
                int hdc = _Export.StartExporting();                
                _View.Output(hdc, resolution, ref userRECT, _View.Extent, null);
                _Export.FinishExporting();
                _Export.Cleanup();
                LogHelper.WriteLog("图幅[" + code + "]导出成功。");
                return true;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(new LogMessage()
                {
                    Message = "图幅[" + code + "]导出失败：" + ex.Message,
                    LogType = zjgisLogType.zjgisLogError
                });
                return false;
            }
        }


    }
}
